/**

* <p>Title: GoodsController.java</p>

* <p>Description: </p>

* <p>Copyright: Copyright (c) 2021</p>

* <p>Company: www.chinasofti.com</p>

* @author knowno

* @date 2021年3月9日

* @version 1.0

*/
package com.etc.ssm.controller;

import com.etc.ssm.entity.AjaxResponse;
import com.etc.ssm.entity.Goods;
import com.etc.ssm.service.GoodsService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 
 * <p>
 * Title: GoodsController
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * @author knowno
 * 
 * @date 2021年3月9日
 * 
 */
@Controller
public class GoodsController {

	@Autowired
	private GoodsService goodsService;

	/**
	 * 
	 * 
	 * <p>
	 * Title: listGoods
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param keywords
	 * @param page
	 * @param limit
	 * @return
	 */ 
	@GetMapping("api/goods")
	@ResponseBody
	public AjaxResponse<Goods> listGoods(
		   @RequestParam(value = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(value = "page", required = false, defaultValue = "1") int page,
			@RequestParam(value = "limit", required = false, defaultValue = "10") int limit) {
		// 调用业务层方法实现分页查询
		PageInfo<Goods> pageinfo = goodsService.showGoods(page, limit, keywords);
		// 将返回结果转换为视图层能接受的数据格式，AjaxResponse=>Layui所以自己封装了一个对象
		AjaxResponse<Goods> ar = new AjaxResponse<Goods>(0, "success", (int) pageinfo.getTotal(), pageinfo.getList());

		return ar;
	}

	@GetMapping("goodslist")
	public String toGoodList(){
		return "back/goods-list";
	}


	@GetMapping("locktest1/{goodsid}")
	@ResponseBody
	public  Goods locktest1(@PathVariable("goodsid") int goodsid){

		Goods goods = goodsService.getGoodsById(goodsid);
		System.out.println(goods.getVersion());

		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		goods.setGoodsprice(300d);
		goodsService.updateGoods(goods);
		return goods;

	}

	@GetMapping("locktest2/{goodsid}")
	@ResponseBody
	public  Goods locktest2(@PathVariable("goodsid") int goodsid){

		Goods goods = goodsService.getGoodsById(goodsid);
		System.out.println(goods.getVersion());


		goods.setGoodsprice(310d);
		goodsService.updateGoods(goods);
		return goods;

	}

}
